<?php
App::uses('AppController', 'Controller');
/**
 * Courses Controller
 *
 * @property Course $Course
 * @property PaginatorComponent $Paginator
 */
class CoursesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Course->recursive = 0;
		$this->set('courses', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Course->exists($id)) {
			throw new NotFoundException(__('Invalid course'));
		}
		$options = array('conditions' => array('Course.' . $this->Course->primaryKey => $id));
		$this->set('course', $this->Course->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->Course->create();
            $this->request->data['Course']['create_date']=date('Y/m/d');
			
			if ($this->Course->save($this->request->data)) {
			   	$this->Session->setFlash(__('Thêm thành công '),'backend/green',array('redirect'=>''));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('Lỗi save dữ liệu'),'backend/red',array('redirect'=>''));
			}
		}
        else{
            $this->loadModel('TypeCourse');
            $typecouses = $this->TypeCourse->find('list',array("fields"=>array('TypeCourse.id','TypeCourse.type_courses_name')));
            $this->set(compact('typecouses'));  
        }
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->Course->exists($id)) {
			throw new NotFoundException(__('Invalid course'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
		    $this->request->data['Course']['update_date']=date('Y/m/d');
			if ($this->Course->save($this->request->data)) {
				$this->Session->setFlash(__('Sửa thành công '),'backend/green',array('redirect'=>''));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('Lỗi save dữ liệu'),'backend/red',array('redirect'=>''));
			}	
      	} else {
      	    $this->loadModel('TypeCourse');
            $typecouses = $this->TypeCourse->find('list',array("fields"=>array('TypeCourse.id','TypeCourse.type_courses_name')));
            $this->set(compact('typecouses'));  
			$options = array('conditions' => array('Course.' . $this->Course->primaryKey => $id));
			$this->request->data = $this->Course->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Course->id = $id;
		if (!$this->Course->exists()) {
			throw new NotFoundException(__('Invalid course'));
		}
		$this->request->onlyAllow('get', 'admin_delete');
		
		if ($this->Course->delete()) {
			$this->Session->setFlash(__('Xóa thành công'),'backend/green',array('redirect'=>''));
		} else {
			$this->Session->setFlash(__('Lỗi xóa dữ liệu'),'backend/red',array('redirect'=>''));
		}
		return $this->redirect(array('action' => 'admin_index'));
	}}
